<?php

namespace app\common\model;

use think\Db;
use think\Log;
use think\Model;


class Frozenlog extends Model
{
    // 表名
    protected $name = 'user_frozen_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];



    /**
     * 变更会员余额
     * @param int    $money   余额
     * @param int    $user_id 会员ID
     * @param int    $order_id 订单ID
     * @param string $memo    备注
     */
    public static function money($money, $user_id, $memo, $order_id, $o_g_id)
    {

        Db::startTrans();
        try {
            $user = User::lock(true)->find($user_id);

            if ($user && $money != 0) {
            
                //更新会员信息
                //写入日志
                self::create(['user_id' => $user_id, 'money' => $money,  'memo' => $memo, 'order_id' => $order_id, 'o_g_id' => $o_g_id]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Log::error('回调失败',$e->getMessage());
            Db::rollback();
        }
    }
    public function ordergoods()
    {
        return $this->belongsTo('app\common\model\order\Goods', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function order()
    {
        return $this->belongsTo('app\common\model\order\Order', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
